package com.woniuxy.creditsystem.common.mapper;

import com.woniuxy.creditsystem.common.bean.PageRequest;
import com.woniuxy.creditsystem.common.bean.RoleInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * 角色的增、删、查、改
 * @author hejie
 */
@Mapper
public interface RoleMapper {

    /**
     * 新增角色
     * @author hejie
     * @param RoleInfo
     * @return 受影响的行数
     */
    //ceator的话：用shiro那个session中的用户名作为它
    @Options(
            useGeneratedKeys = true,
            keyProperty = "r.id"
    )
    @Insert("insert into role_info " +
            "(role_desc,role,createor,create_time,modify_time,status,del_flag)" +
            " values(#{r.roleDesc},#{r.roleDesc},'default',now(),now(),#{r.status},'1')")
    int saveRoleInfo(@Param("r") RoleInfo RoleInfo);

    /**
     * 修改角色
     * @author hejie
     * @param roleInfo
     * @return 受影响的行数
     */
    @Update("<script>" +
            "update role_info" +
            "<set>" +
            "<if test='r.role != null'>" +
            " role = #{r.role}," +
            "</if>" +
            "<if test='r.roleDesc != null'>" +
            " role_desc = #{r.roleDesc}," +
            "</if>" +
            "<if test='r.status != null'>" +
            " status = #{r.status}," +
            "</if>" +
            "modify_time=now()"+
            "</set>" +
            "where id = #{r.id}" +
            "</script>")
    int updateRoleInfo(@Param("r") RoleInfo roleInfo);

    /**
     * 查询所有的角色--只查询有效的1
     * @author hejie
     * @return
     */
    @ResultType(RoleInfo.class)
    @Select("select id,role,role_desc as roleDesc from role_info where status = 1")
    List<RoleInfo> findAll();

    /***
     * 获取一个角色信息
     * @author hejie
     * @param id
     * @return
     */
    @ResultType(RoleInfo.class)
    @Select("select id,role,role_desc roleDesc from role_info where status = 1 and id = #{id}")
    RoleInfo getOneById(@Param("id") Long id);

    /**
     * 分页查询（同时具有模糊查询功能（因为动态sql））
     * @author hejie
     * @param roleInfo
     * @return
     */
    List<RoleInfo> findAllByParams( @Param("r") RoleInfo roleInfo);

    /**
     * 删除管角色
     * @author hejie
     * @param id
     * @return 受影响的行数
     */

    @Delete("delete from role_info where id = #{id}")
    int deleteRoleInfo(@Param("id")Long id);

    /**
     * 根据角色ID查询菜单权限信息，以及功能权限信息
     * @author hejie
     * @param id
     * @return
     */
    @ResultType(Map.class)
    @Select("select m.menu_name as menuName,rm.opt_perm as optPerm from menu_info as m left join role_menu_info as rm on m.id = rm.fk_menu_id where fk_role_id = #{id}")
    List<Map> findPermissionByRoleId(@Param("id")Long id);
}
